<?php
//生成验证码
header("content-type:image/jpeg;");
session_start();
//创建画布
$width=300;
$height=100;
$image=imagecreate($width, $height);
$bg= imagecolorallocate($image,mt_rand(0, 255),mt_rand(0, 255),mt_rand(0, 255));
//制作验证码
$strbase='0123456789';
//$strbase='0123456789abcdefghijklmnopqrstuvwxyz';//生成数字和字母混合验证码
for($i=0;$i<4;$i++){
	$one=$strbase{mt_rand(0, strlen($strbase)-1)};
	$str .= $one;
}
$_SESSION["verify"] = $str;
//将验证码数字 写入图片
for($i=0;$i<strlen($str);$i++){
	$color = imagecolorallocate($image,mt_rand(0, 255),mt_rand(0, 255),mt_rand(0,255));
	imagettftext($image,mt_rand(98, 108),mt_rand(-15, 15),40+60*$i,90,$color,"C:/Windows/Fonts/simhei.ttf" , $str{$i});
}
//制造干扰元素-点
for($i=0;$i<150;$i++){
	$color = imagecolorallocate($image, mt_rand(0, 255), mt_rand(0, 255),mt_rand(125,255));
	imagesetpixel($image, mt_rand(0, 300), mt_rand(0, 100), $color);
}
//制造线段
for($i=0;$i<10;$i++){
	$color = imagecolorallocate($image, mt_rand(0, 255), mt_rand(0, 255),mt_rand(125,255));
	imageline($image, mt_rand(0, 300), mt_rand(0, 100),mt_rand(0, 300), mt_rand(0, 100), $color);
}
imagejpeg($image);//输出图片
imagedestroy($image);//销毁图片资源